package com.wujp.common.template.tools.actuator;

import com.alibaba.fastjson.JSON;
import com.wujp.common.template.tools.cache.BeanFactoryCache;
import com.wujp.common.template.tools.context.BaseProcessContext;
import com.wujp.common.template.tools.enums.BeanType;
import com.wujp.common.template.tools.exception.ProcessException;
import com.wujp.common.template.tools.interfaces.ActionActuator;
import com.wujp.common.template.tools.interfaces.BeanFactory;
import com.wujp.common.template.tools.interfaces.TemplateActuator;
import com.wujp.common.template.tools.interfaces.action.Action;
import com.wujp.common.template.tools.interfaces.context.ProcessContext;
import com.wujp.common.template.tools.interfaces.converter.ProcessContextConverter;
import com.wujp.common.template.tools.model.action.AcionModel;
import com.wujp.common.template.tools.model.action.SerialTemplateActionModel;
import com.wujp.common.template.tools.model.template.SerialTemplateModel;
import com.wujp.common.template.tools.utils.StringUtil;

/**
 * @desc: 串行模板的动作执行器
 * @author: wujp
 * @create: 2023-01-18 11:08
 */
public class SerialTemplateActionActuator implements ActionActuator {

    @Override
    public void exec(AcionModel acionModel, ProcessContext processContext) {
        if (!(acionModel instanceof SerialTemplateActionModel)) {
            throw new ProcessException("SerialTemplateActionActuator模板执行器和配置的模板actionModel不匹配。请确认配置。" + "baseAcionModel=" + JSON.toJSONString(acionModel));
        }
        SerialTemplateActionModel actionModel = (SerialTemplateActionModel) acionModel;

        if (!(processContext instanceof BaseProcessContext)) {
            throw new ProcessException("SerialTemplateActionActuator模板执行器和配置的模板执行上下文不匹配。请确认配置。" + "baseAcionModel=" + JSON.toJSONString(processContext));
        }
        BaseProcessContext baseProcessContext = (BaseProcessContext) processContext;

        String beanType = actionModel.getActionType();
        String beanName = actionModel.getActionName();

        BeanFactory beanFactory = BeanFactoryCache.beanFactorys.get(beanType);
        if (beanFactory == null) {
            throw new ProcessException("bean初始化异常beanType=" + beanType + "没有注册该类型bean的处理工厂。");
        }

        if (BeanType.TEMPLATE_BEAN.getType().equals(beanType)) {
            SerialTemplateModel serialTemplateModel = beanFactory.getBean(beanName);
            if (baseProcessContext.getBaseTemplateModel().getTemplateId().equals(serialTemplateModel.getTemplateId())) {
                throw new ProcessException("模板bean配置了当前模板id为死循环模板。请确认配置。" + "actionName=" + actionModel.getActionName());
            }
            TemplateActuator templateActuator = beanFactory.getBean(beanType);
            if (StringUtil.isNotBank(actionModel.getConverterType())) {
                BeanFactory converterBeanFactory = BeanFactoryCache.beanFactorys.get(actionModel.getConverterType());
                ProcessContextConverter converter = converterBeanFactory.getBean(actionModel.getConverterName());
                ProcessContext subContext = converter.convert(baseProcessContext);
                templateActuator.exec(subContext);
            }

        } else if (BeanType.ClASS_BEAN.getType().equals(beanType) || BeanType.SPRING_BEAN.getType().equals(beanType)) {
            Action action = beanFactory.getBean(beanName);
            if (action == null) {
                throw new ProcessException("bean为空actionType=" + actionModel.getActionType() + "actionName=" + actionModel.getActionName());
            }
            action.exec(baseProcessContext.getBizContext());
        } else {
            throw new ProcessException("bean未正常初始化actionType=" + actionModel.getActionType() + "BaseActionActuator执行器不支持该类型的bean处理。");
        }

    }
}
